package com.kitty.game.equip.handler;

import com.kitty.game.config.Equip;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.message.RespUpgradeInheritPreview;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.JsonUtils;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.EquipUpgradeLevelChangeEvent;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import com.kitty.game.equip.handler.UpgradeHandler;

@Component("EquipInheritSelectHandler")
public class EquipInheritSelectHandler extends UpgradeHandler{
    /**
     * 开始打造 返回打造成功或者失败
     *
     * @param role
     * @param params
     */
    @Override
    public String doUpgrade(Role role, Object... params) {
        return null;
    }

    /**
     * 入口
     *
     * @param role
     * @param upgradePos
     * @param param
     */
    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip mEquip =equipService.getRoleEquipByPos(role,upgradePos);
        RoleEquip oEquip =equipService.getRoleEquipByPos(role,Integer.parseInt(param));
        RespUpgradeInheritPreview preview =new RespUpgradeInheritPreview();
        preview.setFlag(true);
        int coin = 3000000;// equipService.getScore(mEquip,oEquip);
        preview.setMoney(0);
        preview.setPos((short) upgradePos);
        preview.setPara(param);

        Equip mEquipSet = EquipDataPool.getByName(mEquip.getName());
        Equip oEquipSet = EquipDataPool.getByName(oEquip.getName());

        RoleEquip mainEquip =JsonUtils.cloneObject(mEquip);
        RoleEquip otherEquip =JsonUtils.cloneObject(oEquip);
        if (oEquipSet.getPosition() == Const.POS_WEAPON){
            if (oEquip.queryUpgradeLevel() == 12){
                coin = 18000000;
            }else if (oEquip.queryUpgradeLevel() == 11){
                coin = 15000000;
            }else if (oEquip.queryUpgradeLevel() == 10){
                coin = 12000000;
            }else if (oEquip.queryUpgradeLevel() == 9){
                coin = 10000000;
            }else if (oEquip.queryUpgradeLevel() == 8){
                coin = 8000000;
            }else if (oEquip.queryUpgradeLevel() == 7){
                coin = 6000000;
            }
        }else {
            if (oEquip.queryUpgradeLevel() == 12){
                coin = 15000000;
            }else if (oEquip.queryUpgradeLevel() == 11){
                coin = 12000000;
            }else if (oEquip.queryUpgradeLevel() == 10){
                coin = 10000000;
            }else if (oEquip.queryUpgradeLevel() == 9){
                coin = 8000000;
            }else if (oEquip.queryUpgradeLevel() == 8){
                coin = 6000000;
            }else if (oEquip.queryUpgradeLevel() == 7){
                coin = 4000000;
            }
        }
        coin =coin/30;
        preview.setCoin(coin);
        int mLevel = mainEquip.queryUpgradeLevel();
        int mProgress = mainEquip.queryUpgradeProgress();
        int oLevel = otherEquip.queryUpgradeLevel();
        int oProgress = otherEquip.queryUpgradeProgress();

        mainEquip.alterUpgradeLevel(oEquip.queryUpgradeLevel());
        mainEquip.alterUpgradeProgress(oProgress);
        equipService.addUpgradeField(mEquipSet,mainEquip);

        otherEquip.alterUpgradeLevel(mEquip.queryUpgradeLevel());
        otherEquip.alterUpgradeProgress(mProgress);
        equipService.addUpgradeField(oEquipSet,otherEquip);
        preview.setMEquip(equipService.transferList(mainEquip.getFields()));
        preview.setOEquip(equipService.transferList(otherEquip.getFields()));
        MessagePusher.pushMessage(role,preview);

        /**产生一个装备改造等级改变事件*/
        EventDispatcher.getInstance().fireEvent(new EquipUpgradeLevelChangeEvent(EventType.EQUIP_UPGRADE_LEVEL_CHANGE, role, mainEquip, (short) mLevel, mainEquip.queryUpgradeLevel()));
        EventDispatcher.getInstance().fireEvent(new EquipUpgradeLevelChangeEvent(EventType.EQUIP_UPGRADE_LEVEL_CHANGE, role, otherEquip, (short) oLevel, otherEquip.queryUpgradeLevel()));
        return null;
    }

    /**
     * 检查需要的资源是否足够
     *
     * @param objects
     */
    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    /**
     * 打造需要的金钱
     * pos 只在鉴定的时候用
     *
     * @param level
     * @param pos
     */
    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    /**
     * 取操作类型的字符串
     */
    @Override
    public String getUpdateTypeStr() {
        return null;
    }

}
